//
//  RecordCardCell.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/17.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class RecordCardCell: UITableViewCell {

    fileprivate let borderView = UIView() //边框视图
    fileprivate let headView = UIView() //渐变视图
    fileprivate let whiteView = UIView()
    fileprivate let bankImageView = UIImageView() //银行图片
    fileprivate let titleLabel = UILabel() //标题
    fileprivate let recordImageView = UIImageView() //记录图片
    fileprivate let timeTipLabel = UILabel()
    fileprivate let timeLabel = UILabel() //时间
    fileprivate let stateTipLabel = UILabel()
    fileprivate let stateLabel = UILabel() //申请状态
    let inquireBtn = UIButton() //进度查询
    let goonBtn = UIButton() //继续申请
    
    var model : RecordCardModel! {
        
        didSet{
            updateData()
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.contentView.backgroundColor = UIColor.clear
        
        borderView.layer.cornerRadius = 5
        borderView.layer.masksToBounds = true
        borderView.layer.borderWidth = 1
        borderView.layer.borderColor = UIColor.lineColor().cgColor
        self.contentView.addSubview(borderView)

        borderView.addSubview(headView)

        whiteView.backgroundColor = UIColor.white
        whiteView.layer.cornerRadius = RATIO_H(maxNum: 15)
        whiteView.layer.masksToBounds = true
        headView.addSubview(whiteView)
        
        whiteView.addSubview(bankImageView)
        
        titleLabel.textColor = UIColor.white
        titleLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 18))
        headView.addSubview(titleLabel)

        borderView.addSubview(recordImageView)
        
        timeTipLabel.text = applicationTime + "："
        timeTipLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        borderView.addSubview(timeTipLabel)
        
        timeLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        timeLabel.adjustsFontSizeToFitWidth = true
        borderView.addSubview(timeLabel)
        
        stateTipLabel.text = applicationStatus + "："
        stateTipLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        borderView.addSubview(stateTipLabel)
        
        stateLabel.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        stateLabel.textColor = UIColor.endColor()
        borderView.addSubview(stateLabel)
        
        inquireBtn.layer.cornerRadius = RATIO_H(maxNum: 12)
        inquireBtn.layer.masksToBounds = true
        inquireBtn.setTitle(progressQuery, for: .normal)
        inquireBtn.backgroundColor = UIColor.endColor()
        inquireBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        borderView.addSubview(inquireBtn)
        
        goonBtn.layer.cornerRadius = RATIO_H(maxNum: 12)
        goonBtn.layer.masksToBounds = true
        goonBtn.setTitle(goonApply, for: .normal)
        goonBtn.backgroundColor = UIColor.endColor()
        goonBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 14))
        borderView.addSubview(goonBtn)
        
        let lineView = UIView()
        lineView.backgroundColor = UIColor.lineColor()
        self.contentView.addSubview(lineView)
        
        borderView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 10))
            make.right.equalToSuperview().offset(RATIO(maxNum: -10))
            make.top.equalToSuperview().offset(RATIO(maxNum: 10))
            make.height.equalTo(RATIO_H(maxNum: 160))
        }
        
        headView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 40))
        }
        
        whiteView.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(RATIO(maxNum: 20))
            make.centerY.equalToSuperview()
            make.height.width.equalTo(RATIO(maxNum: 30))
        }
        
        bankImageView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.width.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(whiteView.snp.right).offset(RATIO(maxNum: 10))
            make.centerY.equalTo(whiteView)
            make.width.greaterThanOrEqualTo(10)
            make.height.greaterThanOrEqualTo(10)
        }
        
        recordImageView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom).offset(RATIO_H(maxNum: 20))
            make.left.equalToSuperview().offset(RATIO(maxNum: 5))
            make.width.equalTo(RATIO(maxNum: 120))
            make.height.equalTo(RATIO_H(maxNum: 80))
        }
        
        timeTipLabel.snp.makeConstraints { (make) in
            make.left.equalTo(recordImageView.snp.right).offset(RATIO(maxNum: 20))
            make.top.equalTo(recordImageView)
            make.width.greaterThanOrEqualTo(10)
            make.height.equalTo(RATIO_H(maxNum: 20))
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeTipLabel.snp.right)
            make.top.height.equalTo(timeTipLabel)
            make.width.greaterThanOrEqualTo(10)
        }
        
        stateTipLabel.snp.makeConstraints { (make) in
            make.left.height.equalTo(timeTipLabel)
            make.top.equalTo(timeTipLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.width.greaterThanOrEqualTo(10)
        }
        
        stateLabel.snp.makeConstraints { (make) in
            make.left.equalTo(stateTipLabel.snp.right)
            make.top.height.equalTo(stateTipLabel)
            make.width.greaterThanOrEqualTo(10)
        }
        
        inquireBtn.snp.makeConstraints { (make) in
            make.left.equalTo(stateTipLabel)
            make.top.equalTo(stateTipLabel.snp.bottom).offset(RATIO_H(maxNum: 10))
            make.width.equalTo(RATIO(maxNum: 80))
            make.height.equalTo(RATIO_H(maxNum: 24))
        }

        goonBtn.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(RATIO(maxNum: -5))
            make.centerY.width.height.equalTo(inquireBtn)
        }

        lineView.snp.makeConstraints { (make) in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(1)
            
        }
        
        self.layoutIfNeeded()
        
        //颜色渐变
        let gradientLayer = CAGradientLayer()
        gradientLayer.colors = [UIColor.startBlue().cgColor,UIColor.centerColor().cgColor,UIColor.endColor().cgColor]
        gradientLayer.locations = [0.3,0.5,1.0]
        gradientLayer.startPoint = .init(x: 0, y: 0)
        gradientLayer.endPoint = .init(x: 1.0, y: 0)
        gradientLayer.frame = .init(x: 0, y: 0, width: ScreenW-RATIO(maxNum: 20), height: RATIO_H(maxNum: 40))
        headView.layer.insertSublayer(gradientLayer, at: 0)
        
        
        
    }
    
    //MARK: 更新数据
    fileprivate func updateData(){
        
        if let bank_name = model.bank_name {
            bankImageView.image = UIImage.imageWithName(KMTool.selectBankImage(bankName: bank_name))
        }
        titleLabel.text = model.credit_name ?? ""
        recordImageView.setUrlImage(with: model.credit_img, placeholder: UIImage.imageWithName("loading"))
        timeLabel.text = model.apply_time ?? ""
        stateLabel.text = model.status ?? ""
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
